<?php 
namespace SimplePhp\Rendering;

/**
* DOMNodeList遍历的时候
* length会随着遍历的item指向而减少
* 这里要吧length固定
*/
class DOMNodeListIterator implements \Iterator 
{
	private $position;
	private $list;
	private $length;
	private $NodeList=[];
	public function __construct(\DOMNodeList $list){
		$this->list=$list;
		$this->length=$list->length;
		// $this->position=0;
		$length=$list->length;
		$i=$length-1;
		while ($i>-1) {
			$ele=$list->item($i);
			$this->NodeList[]=$ele;
			$i--;
		}
		$this->NodeList=array_reverse($this->NodeList);
	}
	function rewind() {
	    	$this->position = 0;
	}
	function current() {
	    	return $this->NodeList[$this->position];
	}
	function key() {
		return $this->position;
	}
	function next() {
		++$this->position;
	}
	function valid() {
		if($this->position>=$this->length){
			return false;
		}else{
			return true;
		}
	}
}